package com.atls.eduservice.controller;


import com.atls.commonutils.R;
import com.atls.eduservice.entity.EduChapter;
import com.atls.eduservice.entity.chapter.ChapterVo;
import com.atls.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-01-30
 */

@Api("章节管理")
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {

   @Autowired
   private EduChapterService chapterService;

   //查询课程大纲列表
   @ApiOperation("课程列表大纲")
   @GetMapping("getChapterVideo/{courseId}")
   public R getChapterVideo(@PathVariable String courseId){

       List<ChapterVo> list=chapterService.getChapterVideoByCourseId(courseId);
       return R.ok().data("allChapterVideo",list);
   }


   //添加章节
   @ApiOperation("添加章节")
   @PostMapping("addChapter")
   public R addChapter(@RequestBody EduChapter eduChapter){
        chapterService.save(eduChapter);
        return R.ok();
   }

   //根据id查询章节
   @GetMapping("getChapterInfo/{chapterId}")
   public R getChapterInfo(@PathVariable String chapterId){
       EduChapter eduChapter = chapterService.getById(chapterId);
       return R.ok().data("eduChapter",eduChapter);
   }

   //修改章节
   @PostMapping("updateChapter")
   public R updateChapter(@RequestBody EduChapter eduChapter){
        chapterService.updateById(eduChapter);
        return R.ok();
   }

   //删除章节
   @DeleteMapping("{chapterId}")
   public R deleteChapter(@PathVariable String chapterId){
     boolean flag=chapterService.deleteChapter(chapterId);
     if(flag){
         return R.ok();
     }else{
         return R.error();
     }
   }
}

